<?php
/**
 * Created by PhpStorm.
 * User: sunshine
 * Date: 2016/8/31
 * Time: 8:41
 */
require 'Admin/Model/Model.php';
class D_testModel extends Model{
    function __Construct(){
        parent::__Construct();
    }
    function getTestList(){
        $sql = "select * from d_test";
        $data = $this->conn->query($sql);
        return $data;
    }
    function addTest($data){
      //  var_dump($data);
        if(!empty($_FILES["pic1"]["name"])){
            $pic1 = $this->uploadfile('pic1','./upload/',array('jpg','gif','png'));
        }
        else{
            $pic1='';
        }
        if(!empty($_FILES["pic2"]["name"])){
            $pic2 = $this->uploadfile('pic2','./upload/',array('jpg','gif','png'));
        }
        else{
            $pic2='';
        }
        if(!empty($_FILES["pic3"]["name"])){
            $pic3 = $this->uploadfile('pic3','./upload/',array('jpg','gif','png'));
        }
        else{
            $pic3='';
        }
        if(!empty($_FILES["pic4"]["name"])){
            $pic4 = $this->uploadfile('pic4','./upload/',array('jpg','gif','png'));
        }
        else{
            $pic4='';
        }

        $test = array(
            'pid'=>$data['pid'],
            'content' => trim($data['content']),
            'score'=>$data['score'],
            'op1'=>$data['op1'],
            'pic1'=>$pic1,
            'op2'=>$data['op2'],
            'pic2'=>$pic2,
            'op3'=>$data['op3'],
            'pic3'=>$pic3,
            'op4'=>$data['op4'],
            'pic4'=>$pic4,
            'ans'=>$data['ans']
        );
     //   var_dump($test);
        $keys = implode(',',array_keys($test));
        $values = implode("','", array_values($test));
        $sql="insert into d_test ({$keys}) VALUES ('{$values}')";
        //echo mysql_query($sql)."65s4";
        //执行SQL语句
       // echo $sql;
        $d = $this->conn->query($sql);
        return $d;
    }
    function delTest($id){
        $sql1 = "select * from d_test where d_id=".$id;
        $data1 = $this->conn->query($sql1);
        $filepath1="./upload/".$data1['pic1'];
        if(!empty($data1['pic1']))
        {
            unlink($filepath1);
            echo "图片1删除成功".'<br>';
        }
        $filepath2="./upload/".$data1['pic2'];
        if(!empty($data1['pic2']))
        {
            unlink($filepath2);
            echo "图片2删除成功".'<br>';
        }
        $filepath3="./upload/".$data1['pic3'];
        if(!empty($data1['pic3']))
        {
            unlink($filepath3);
            echo "图片3删除成功".'<br>';
        }
        $filepath4="./upload/".$data1['pic4'];
        if(!empty($data1['pic4']))
        {
            unlink($filepath4);
            echo "图片4删除成功".'<br>';
        }
        $sql = "delete from d_test where d_id=".$id;
        $result = $this->conn->query($sql);
        return $result;
    }

}
?>